package com.hao.netty.client;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

/**
 * TODO
 *
 * @author 97472
 * @version 1.0
 * @date 2021/11/25 16:30
 */
public class NettyClientHandler extends ChannelInboundHandlerAdapter {

    // 当连接完成后的事件
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("ctx = " + ctx);
        ctx.writeAndFlush(Unpooled.copiedBuffer("hello 服务端...", CharsetUtil.UTF_8));
    }

    // 当有读取事件发生后
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("ctx = " + ctx);
        ByteBuf byteBuf = (ByteBuf) msg;
        System.out.println("msg = " + byteBuf.toString(CharsetUtil.UTF_8));
    }
}
